package priv.pront.code.mati.province;

import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-11-17 10:20
 */
public class MC0206_小码哥的英语 {

    static int n;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        while (n-- > 0) {
            String s = scanner.next();
            char[] chs = s.toCharArray();
            int count1 = 0, count2 = 0;
            for (int i = 0; i <chs.length; i++) {
                if(i % 2 == 0 && chs[i] != 'A') count1++;
                if(i % 2 == 1 && chs[i] != 'B') count1++;
                if(i % 2 == 0 && chs[i] != 'B') count2++;
                if(i % 2 == 1 && chs[i] != 'A') count2++;
            }
            System.out.println(Math.min(count1,count2));
        }
    }
}
